local tempo_de_debuff = 8*TICRATE

freeslot(
	"MT_DEBUFF_CONFUSION",
		"SPR_DFCF",
			"S_DEBUFF_CONFUSION"
)

mobjinfo[MT_DEBUFF_CONFUSION]={
	doomednum = -1,
	spawnstate = S_DEBUFF_CONFUSION,
	spawnhealth = 1000,
	reactiontime = 1,
	radius = 13*FRACUNIT,
	height = 14*FRACUNIT,
	mass = 100,
	damage = 0,
	flags = MF_NOGRAVITY|MF_NOCLIP
}

states[S_DEBUFF_CONFUSION]={SPR_DFCF, A, tempo_de_debuff, None, 0, 0, S_NULL}

local rdm = P_RandomRange(1, 3)	

local function no_control(p)

	p.debuff_confused_control = true
	
	if rdm == 1
		//up->right
		COM_BufInsertText(p, tostring("setcontrol \"forward\" \"d\" \"RIGHT ARROW\""))
		//right->down
		COM_BufInsertText(p, tostring("setcontrol \"straferight\" \"s\" \"DOWN ARROW\""))
		//down->left
		COM_BufInsertText(p, tostring("setcontrol \"backward\" \"a\" \"LEFT ARROW\""))
		//left->up
		COM_BufInsertText(p, tostring("setcontrol \"strafeleft\" \"w\" \"UP ARROW\""))
		//Gamepad
		COM_BufInsertText(p, tostring("joyaxis_move \"X-Axis\""))
		COM_BufInsertText(p, tostring("joyaxis_side \"Y-Axis\""))
	elseif rdm == 2
		//up->down
		COM_BufInsertText(p, tostring("setcontrol \"forward\" \"s\" \"DOWN ARROW\""))
		//right->left
		COM_BufInsertText(p, tostring("setcontrol \"straferight\" \"a\" \"LEFT ARROW\""))
		//down->up
		COM_BufInsertText(p, tostring("setcontrol \"backward\" \"w\" \"UP ARROW\""))
		//left->right
		COM_BufInsertText(p, tostring("setcontrol \"strafeleft\" \"d\" \"RIGHT ARROW\""))
		//Gamepad
		COM_BufInsertText(p, tostring("joyaxis_move \"X-Axis\""))
		COM_BufInsertText(p, tostring("joyaxis_side \"Y-Axis\""))
	elseif rdm == 3
		//up->left
		COM_BufInsertText(p, tostring("setcontrol \"forward\" \"a\" \"LEFT ARROW\""))
		//right->up
		COM_BufInsertText(p, tostring("setcontrol \"straferight\" \"w\" \"UP ARROW\""))
		//down->right
		COM_BufInsertText(p, tostring("setcontrol \"backward\" \"d\" \"RIGHT ARROW\""))
		//left->down
		COM_BufInsertText(p, tostring("setcontrol \"strafeleft\" \"s\" \"DOWN ARROW\""))
		//Gamepad
		COM_BufInsertText(p, tostring("joyaxis_move \"X-Axis\""))
		COM_BufInsertText(p, tostring("joyaxis_side \"Y-Axis\""))
	end
	
	if (p.debuff_confused_tempo_de_debuff == 0 and p.debuff_confused_control == true)
		COM_BufInsertText(p, tostring("setcontrol \"forward\" \"w\" \"UP ARROW\""))
		COM_BufInsertText(p, tostring("setcontrol \"straferight\" \"d\" \"RIGHT ARROW\""))
		COM_BufInsertText(p, tostring("setcontrol \"backward\" \"s\" \"DOWN ARROW\""))
		COM_BufInsertText(p, tostring("setcontrol \"strafeleft\" \"a\" \"LEFT ARROW\""))
		COM_BufInsertText(p, tostring("joyaxis_move \"Y-Axis\""))
		COM_BufInsertText(p, tostring("joyaxis_side \"X-Axis\""))
		p.debuff_confused = false
		p.debuff_confused_control = false
	end
end

local function debuff_confusion(p)
	
	if p.debuff_confused == nil then p.debuff_confused = false end
	if p.debuff_confused_control == nil then p.debuff_confused_control = false end
	if p.debuff_confused_tempo_de_debuff == nil then p.debuff_confused_tempo_de_debuff = 0 end
	
	if p.debuff_confused_tempo_de_debuff > 0
		p.debuff_confused_tempo_de_debuff = $ - 1
	end
	
	if (p.debuff_confused == true)
		if p.debuff_confused_control == false then p.debuff_confused_tempo_de_debuff = tempo_de_debuff end
		//p.pflags = $|PF_JUMPSTASIS	
		no_control(p)
	end
	if (p.debuff_confused == true and p.debuff_confused_control == true)
		if p.debuff_confused_tempo_de_debuff == tempo_de_debuff
			p.debuff_confused_icon = P_SpawnMobj(p.mo.x, p.mo.y, (p.mo.z + p.mo.height + 10*FRACUNIT), MT_DEBUFF_CONFUSION)
		end
		if p.debuff_confused_icon ~= nil
			P_TeleportMove(p.debuff_confused_icon, p.mo.x, p.mo.y, (p.mo.z + p.mo.height + 10*FRACUNIT))
		end
	end
end

addHook("ThinkFrame", do
	for p in players.iterate
		if not (p or p.mo)
			continue
		else
			//debuff_confusion(p)
		end
	end
end)